package com.yhgarden.bbs.service.impl.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yhgarden.bbs.entity.system.BaseSelectItem;
import com.yhgarden.bbs.mapper.system.BaseSelectItemMapper;
import com.yhgarden.bbs.model.enums.StatusCode;
import com.yhgarden.bbs.model.helper.Pager;
import com.yhgarden.bbs.model.helper.SortPointer;
import com.yhgarden.bbs.model.response.BaseResponse;
import com.yhgarden.bbs.service.api.system.BaseSelectItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.UUID;

@Service
public class BaseSelectItemServiceImpl implements BaseSelectItemService {

    @Autowired
    private BaseSelectItemMapper selectItemMapper;


    @Override
    public BaseResponse createOrUpdate(BaseSelectItem selectItem) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        if(selectItem.getId() == null){
            String uuid = UUID.randomUUID().toString();
            selectItem.setId(uuid);
            selectItemMapper.create(selectItem);
        }else {
            selectItemMapper.update(selectItem);
        }
        response.setData(selectItem);
        return response;
    }


    @Override
    public BaseResponse getById(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        BaseSelectItem selectItem = selectItemMapper.getById(id);
        response.setData(selectItem);
        return response;
    }


    @Override
    public BaseResponse getByPid(String pid) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        List<BaseSelectItem> selectItemList = selectItemMapper.getByPid(pid);
        response.setData(selectItemList);
        return response;
    }


    @Override
    public BaseResponse getByIdMoreDetail(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        BaseSelectItem selectItem = selectItemMapper.getByIdMoreDetail(id);
        response.setData(selectItem);
        return response;
    }


    @Override
    public BaseResponse query(BaseSelectItem selectItem) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = selectItem.getPager();
        SortPointer sortPointer = selectItem.getSortPointer();
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<BaseSelectItem> selectItemList = selectItemMapper.query(selectItem,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<BaseSelectItem>(selectItemList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse queryMoreDetail(BaseSelectItem selectItem) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        Pager pager = selectItem.getPager();
        SortPointer sortPointer = selectItem.getSortPointer();
        if(sortPointer == null){
            sortPointer = new SortPointer();
        }
        PageHelper.startPage(pager.getCurrentPage(),pager.getPageSize());
        List<BaseSelectItem> selectItemList = selectItemMapper.queryMoreDetail(selectItem,sortPointer.getOrder(),sortPointer.getField());
        PageInfo result = new PageInfo<BaseSelectItem>(selectItemList);
        response.setData(result);
        return response;
    }


    @Override
    public BaseResponse delete(String id) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        selectItemMapper.delete(id);
        return response;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResponse batchDelete(List<String> ids) {
        BaseResponse response = new BaseResponse(StatusCode.SUCCESS);
        selectItemMapper.batchDelete(ids);
        return response;
    }
}
